package com.demo.project_template.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.demo.project_template.entity.Product;
import com.demo.project_template.entity.ProductCategory;
import com.demo.project_template.mapper.ProductCategoryMapper;
import com.demo.project_template.mapper.ProductMapper;
import com.demo.project_template.service.ProductService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.demo.project_template.vo.response.ProductVO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 商品表 服务实现类
 * </p>
 *
 * @author 杨玉龙
 * @since 2023-06-19 15:56:43
 */
@Service
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> implements ProductService {
    @Resource
    private ProductMapper productMapper;
    @Resource
    private ProductCategoryMapper productCategoryMapper;

    @Override
    public List<ProductVO> selectAllProducts() {
        List<ProductCategory> productCategories = productCategoryMapper.selectList(null);
        List<ProductVO> productVOS = new ArrayList<>();
        for (ProductCategory productCategory : productCategories) {
            ProductVO productVO = new ProductVO();
            productVO.setProductCategoryId(productCategory.getCategoryId());
            productVO.setProductCategoryName(productCategory.getCategoryName());
            productVO.setProducts(productMapper.selectList(new LambdaQueryWrapper<Product>().eq(Product::getProductCategoryId, productCategory.getCategoryId())));
            productVOS.add(productVO);
        }
        return productVOS;
    }
}
